package  org.cocos2dx.javascript.Advert.gdt;

import android.app.Activity;
import android.util.Log;
import android.widget.FrameLayout;

import com.qq.e.ads.splash.SplashAD;
import com.qq.e.ads.splash.SplashADListener;
import com.qq.e.comm.util.AdError;
import org.cocos2dx.javascript.Advert.core.AdBase;

public class GDTSplashAd extends AdBase implements SplashADListener {

    private SplashAD mSplashAD;
    private FrameLayout mContainer;
    private FrameLayout mRootView;

    @Override
    public void loadAd(Activity activity, boolean show) {
        mActivity = activity;
        mShow = show;
        if (mSplashAD == null) {
            mSplashAD = new SplashAD(mActivity, GDTConstants.SplashPosID, this);
        }
        mSplashAD.fetchAdOnly();
    }

    public void showAd(Activity activity) {
        if (!mLoaded) {
            loadAd(activity, true);
            return;
        }
        mLoaded = false;
        if (mContainer == null) {
            mContainer = new FrameLayout(mActivity);
            mRootView = (FrameLayout) mActivity.findViewById(android.R.id.content).getRootView();
            mRootView.addView(mContainer);
        }
        mSplashAD.showAd(mContainer);
    }

    @Override
    public void onADDismissed() {
        if (mRootView != null) {
            mRootView.removeView(mContainer);
            mContainer = null;
        }
    }

    @Override
    public void onNoAD(AdError adError) {
        Log.d("gdt GDTSplashAd", adError.getErrorCode() + " onNoAD " + adError.getErrorMsg());
    }

    @Override
    public void onADPresent() {

    }

    @Override
    public void onADClicked() {

    }

    @Override
    public void onADTick(long l) {

    }

    @Override
    public void onADExposure() {

    }

    @Override
    public void onADLoaded(long l) {
        Log.d("gdt GDTSplashAd", "onADLoaded: ");
        mLoaded = true;
        if (mShow) {
            showAd(mActivity);
        }
    }
}
